{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CaDRReS-Sc Training\n",
    "This notebook show an example of how to fit CaDRReS-Sc model on a training set (e.g GDSC or any pharmacogenomic experiment you might have) and save its final version to make predictions on new data. For the detail of the new objective function, please refer to our manuscript."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T00:58:46.309415Z",
     "start_time": "2020-12-04T00:58:41.915956Z"
    }
   },
   "outputs": [],
   "source": [
    "import sys, os, pickle\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "np.set_printoptions(precision=2)\n",
    "from collections import Counter\n",
    "\n",
    "import importlib\n",
    "from ipywidgets import widgets\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "\n",
    "scriptpath = '..'\n",
    "sys.path.append(os.path.abspath(scriptpath))\n",
    "\n",
    "from cadrres_sc import pp, model, evaluation, utility"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Read data\n",
    "In this step we expect a dataset holding gene expression and screened drug responses. In the following example, we load this information from the GDSC dataset.  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read cell line info\n",
    "\n",
    "- Cell line tissue info\n",
    "- Observed drug response IC50 to be used as ground truth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T00:58:46.466396Z",
     "start_time": "2020-12-04T00:58:46.310869Z"
    }
   },
   "outputs": [],
   "source": [
    "tissue_sample_df = pd.read_csv('../data/GDSC/GDSC_tissue_info.csv', index_col=0)\n",
    "tissue_sample_df.index = tissue_sample_df.index.astype(str)\n",
    "\n",
    "cell_line_obs_df = pd.read_csv('../data/GDSC/gdsc_all_abs_ic50_bayesian_sigmoid_only9dosages.csv', index_col=0)\n",
    "cell_line_obs_df.index = cell_line_obs_df.index.astype(str)\n",
    "\n",
    "# cell lines list which your model will be trained on\n",
    "cell_line_sample_list = cell_line_obs_df.index.astype(str)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In case you want sample weight based on cancer type. In our example we picked head and neck cell lines."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T00:58:46.472574Z",
     "start_time": "2020-12-04T00:58:46.467962Z"
    }
   },
   "outputs": [],
   "source": [
    "cell_line_hn_sample_list = tissue_sample_df[tissue_sample_df['TCGA_CLASS']=='HNSC'].index\n",
    "cell_line_hn_obs_df = cell_line_obs_df.loc[cell_line_hn_sample_list]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read drug info\n",
    "\n",
    "In this example, we focus on 81 drugs that sensitive in head and neck cell lines."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T00:58:46.516448Z",
     "start_time": "2020-12-04T00:58:46.473827Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataframe shape: (226, 27) \n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Drug Name</th>\n",
       "      <th>Synonyms</th>\n",
       "      <th>Target</th>\n",
       "      <th>Target Pathway</th>\n",
       "      <th>Selleckchem Cat#</th>\n",
       "      <th>CAS number</th>\n",
       "      <th>PubCHEM</th>\n",
       "      <th>Others</th>\n",
       "      <th>entropy</th>\n",
       "      <th>max_conc</th>\n",
       "      <th>...</th>\n",
       "      <th>median_ic50_9f</th>\n",
       "      <th>log2_median_ic50_9f</th>\n",
       "      <th>log2_median_ic50_hn</th>\n",
       "      <th>median_ic50_hn</th>\n",
       "      <th>median_ic50_3f_hn</th>\n",
       "      <th>log2_median_ic50_3f_hn</th>\n",
       "      <th>median_ic50_9f_hn</th>\n",
       "      <th>log2_median_ic50_9f_hn</th>\n",
       "      <th>num_sensitive</th>\n",
       "      <th>num_sensitive_hn</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Drug ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Erlotinib</td>\n",
       "      <td>Tarceva, RG-1415, CP-358774, OSI-774, Ro-50823...</td>\n",
       "      <td>EGFR</td>\n",
       "      <td>EGFR signaling</td>\n",
       "      <td>S7786</td>\n",
       "      <td>183321-74-6</td>\n",
       "      <td>176870</td>\n",
       "      <td>(S1023, 183319-69-9, HCl)</td>\n",
       "      <td>7.045609</td>\n",
       "      <td>2.0</td>\n",
       "      <td>...</td>\n",
       "      <td>8.44889</td>\n",
       "      <td>3.078762</td>\n",
       "      <td>7.764640</td>\n",
       "      <td>217.465095</td>\n",
       "      <td>72.488365</td>\n",
       "      <td>6.179678</td>\n",
       "      <td>24.162788</td>\n",
       "      <td>4.594715</td>\n",
       "      <td>17</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1001</th>\n",
       "      <td>AICA Ribonucleotide</td>\n",
       "      <td>AICAR, N1-(b-D-Ribofuranosyl)-5-aminoimidazole...</td>\n",
       "      <td>AMPK agonist</td>\n",
       "      <td>Metabolism</td>\n",
       "      <td>S1802</td>\n",
       "      <td>2627-69-2</td>\n",
       "      <td>65110</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.034272</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>...</td>\n",
       "      <td>206.74838</td>\n",
       "      <td>7.691732</td>\n",
       "      <td>9.939784</td>\n",
       "      <td>982.139588</td>\n",
       "      <td>327.379863</td>\n",
       "      <td>8.354822</td>\n",
       "      <td>109.126621</td>\n",
       "      <td>6.769859</td>\n",
       "      <td>476</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   Drug Name  \\\n",
       "Drug ID                        \n",
       "1                  Erlotinib   \n",
       "1001     AICA Ribonucleotide   \n",
       "\n",
       "                                                  Synonyms        Target  \\\n",
       "Drug ID                                                                    \n",
       "1        Tarceva, RG-1415, CP-358774, OSI-774, Ro-50823...          EGFR   \n",
       "1001     AICAR, N1-(b-D-Ribofuranosyl)-5-aminoimidazole...  AMPK agonist   \n",
       "\n",
       "         Target Pathway Selleckchem Cat#   CAS number PubCHEM  \\\n",
       "Drug ID                                                         \n",
       "1        EGFR signaling            S7786  183321-74-6  176870   \n",
       "1001         Metabolism            S1802    2627-69-2   65110   \n",
       "\n",
       "                            Others   entropy  max_conc  ...  median_ic50_9f  \\\n",
       "Drug ID                                                 ...                   \n",
       "1        (S1023, 183319-69-9, HCl)  7.045609       2.0  ...         8.44889   \n",
       "1001                           NaN  6.034272    2000.0  ...       206.74838   \n",
       "\n",
       "         log2_median_ic50_9f  log2_median_ic50_hn  median_ic50_hn  \\\n",
       "Drug ID                                                             \n",
       "1                   3.078762             7.764640      217.465095   \n",
       "1001                7.691732             9.939784      982.139588   \n",
       "\n",
       "         median_ic50_3f_hn  log2_median_ic50_3f_hn  median_ic50_9f_hn  \\\n",
       "Drug ID                                                                 \n",
       "1                72.488365                6.179678          24.162788   \n",
       "1001            327.379863                8.354822         109.126621   \n",
       "\n",
       "         log2_median_ic50_9f_hn  num_sensitive  num_sensitive_hn  \n",
       "Drug ID                                                           \n",
       "1                      4.594715             17                 1  \n",
       "1001                   6.769859            476                27  \n",
       "\n",
       "[2 rows x 27 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset_drug_df = pd.read_csv('../preprocessed_data/GDSC/drug_stat.csv', index_col=0) # hn_drug_stat | drug_stat\n",
    "dataset_drug_df.index = dataset_drug_df.index.astype(str)\n",
    "\n",
    "dataset_drug_list = dataset_drug_df.index\n",
    "dataset_drug_df.shape\n",
    "print(\"Dataframe shape:\", dataset_drug_df.shape, \"\\n\")\n",
    "dataset_drug_df.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Read gene expression\n",
    "\n",
    "The file can be download from https://www.dropbox.com/s/3v576mspw5yewbm/GDSC_exp.tsv?dl=0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T00:58:52.825077Z",
     "start_time": "2020-12-04T00:58:46.517663Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataframe shape: (17737, 1018) \n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>906826</th>\n",
       "      <th>687983</th>\n",
       "      <th>910927</th>\n",
       "      <th>1240138</th>\n",
       "      <th>1240139</th>\n",
       "      <th>906792</th>\n",
       "      <th>910688</th>\n",
       "      <th>1240135</th>\n",
       "      <th>1290812</th>\n",
       "      <th>907045</th>\n",
       "      <th>...</th>\n",
       "      <th>753584</th>\n",
       "      <th>907044</th>\n",
       "      <th>998184</th>\n",
       "      <th>908145</th>\n",
       "      <th>1659787</th>\n",
       "      <th>1298157</th>\n",
       "      <th>1480372</th>\n",
       "      <th>1298533</th>\n",
       "      <th>930299</th>\n",
       "      <th>905954.1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GENE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>TSPAN6</th>\n",
       "      <td>7.632023</td>\n",
       "      <td>7.548671</td>\n",
       "      <td>8.712338</td>\n",
       "      <td>7.797142</td>\n",
       "      <td>7.729268</td>\n",
       "      <td>7.074533</td>\n",
       "      <td>3.285198</td>\n",
       "      <td>6.961606</td>\n",
       "      <td>5.943046</td>\n",
       "      <td>3.455951</td>\n",
       "      <td>...</td>\n",
       "      <td>7.105637</td>\n",
       "      <td>3.236503</td>\n",
       "      <td>3.038892</td>\n",
       "      <td>8.373223</td>\n",
       "      <td>6.932178</td>\n",
       "      <td>8.441628</td>\n",
       "      <td>8.422922</td>\n",
       "      <td>8.089255</td>\n",
       "      <td>3.112333</td>\n",
       "      <td>7.153127</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TNMD</th>\n",
       "      <td>2.964585</td>\n",
       "      <td>2.777716</td>\n",
       "      <td>2.643508</td>\n",
       "      <td>2.817923</td>\n",
       "      <td>2.957739</td>\n",
       "      <td>2.889677</td>\n",
       "      <td>2.828203</td>\n",
       "      <td>2.874751</td>\n",
       "      <td>2.686874</td>\n",
       "      <td>3.290184</td>\n",
       "      <td>...</td>\n",
       "      <td>2.798847</td>\n",
       "      <td>2.745137</td>\n",
       "      <td>2.976406</td>\n",
       "      <td>2.852552</td>\n",
       "      <td>2.622630</td>\n",
       "      <td>2.639276</td>\n",
       "      <td>2.879890</td>\n",
       "      <td>2.521169</td>\n",
       "      <td>2.870468</td>\n",
       "      <td>2.834285</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 1018 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          906826    687983    910927   1240138   1240139    906792    910688  \\\n",
       "GENE                                                                           \n",
       "TSPAN6  7.632023  7.548671  8.712338  7.797142  7.729268  7.074533  3.285198   \n",
       "TNMD    2.964585  2.777716  2.643508  2.817923  2.957739  2.889677  2.828203   \n",
       "\n",
       "         1240135   1290812    907045  ...    753584    907044    998184  \\\n",
       "GENE                                  ...                                 \n",
       "TSPAN6  6.961606  5.943046  3.455951  ...  7.105637  3.236503  3.038892   \n",
       "TNMD    2.874751  2.686874  3.290184  ...  2.798847  2.745137  2.976406   \n",
       "\n",
       "          908145   1659787   1298157   1480372   1298533    930299  905954.1  \n",
       "GENE                                                                          \n",
       "TSPAN6  8.373223  6.932178  8.441628  8.422922  8.089255  3.112333  7.153127  \n",
       "TNMD    2.852552  2.622630  2.639276  2.879890  2.521169  2.870468  2.834285  \n",
       "\n",
       "[2 rows x 1018 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gene_exp_df = pd.read_csv('../data/GDSC/GDSC_exp.tsv', sep='\\t', index_col=0)\n",
    "print(\"Dataframe shape:\", gene_exp_df.shape, \"\\n\")\n",
    "gene_exp_df.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If there is any gene with mutiple probes, calculate the mean."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T00:58:53.256046Z",
     "start_time": "2020-12-04T00:58:52.826498Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataframe shape: (17419, 1018) \n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>906826</th>\n",
       "      <th>687983</th>\n",
       "      <th>910927</th>\n",
       "      <th>1240138</th>\n",
       "      <th>1240139</th>\n",
       "      <th>906792</th>\n",
       "      <th>910688</th>\n",
       "      <th>1240135</th>\n",
       "      <th>1290812</th>\n",
       "      <th>907045</th>\n",
       "      <th>...</th>\n",
       "      <th>753584</th>\n",
       "      <th>907044</th>\n",
       "      <th>998184</th>\n",
       "      <th>908145</th>\n",
       "      <th>1659787</th>\n",
       "      <th>1298157</th>\n",
       "      <th>1480372</th>\n",
       "      <th>1298533</th>\n",
       "      <th>930299</th>\n",
       "      <th>905954.1</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>GENE</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A1BG</th>\n",
       "      <td>6.208447</td>\n",
       "      <td>5.025810</td>\n",
       "      <td>5.506955</td>\n",
       "      <td>4.208349</td>\n",
       "      <td>3.399366</td>\n",
       "      <td>4.917872</td>\n",
       "      <td>3.828088</td>\n",
       "      <td>5.146903</td>\n",
       "      <td>3.107543</td>\n",
       "      <td>5.062066</td>\n",
       "      <td>...</td>\n",
       "      <td>4.272172</td>\n",
       "      <td>3.435025</td>\n",
       "      <td>4.930052</td>\n",
       "      <td>2.900213</td>\n",
       "      <td>4.523712</td>\n",
       "      <td>5.074951</td>\n",
       "      <td>2.957153</td>\n",
       "      <td>3.089628</td>\n",
       "      <td>4.047364</td>\n",
       "      <td>5.329524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A1CF</th>\n",
       "      <td>2.981775</td>\n",
       "      <td>2.947547</td>\n",
       "      <td>2.872071</td>\n",
       "      <td>3.075478</td>\n",
       "      <td>2.853231</td>\n",
       "      <td>3.221491</td>\n",
       "      <td>2.996355</td>\n",
       "      <td>2.893977</td>\n",
       "      <td>2.755668</td>\n",
       "      <td>2.985650</td>\n",
       "      <td>...</td>\n",
       "      <td>2.941659</td>\n",
       "      <td>3.155536</td>\n",
       "      <td>2.983619</td>\n",
       "      <td>3.118312</td>\n",
       "      <td>2.975409</td>\n",
       "      <td>2.905804</td>\n",
       "      <td>2.944488</td>\n",
       "      <td>2.780003</td>\n",
       "      <td>2.870819</td>\n",
       "      <td>2.926353</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 1018 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        906826    687983    910927   1240138   1240139    906792    910688  \\\n",
       "GENE                                                                         \n",
       "A1BG  6.208447  5.025810  5.506955  4.208349  3.399366  4.917872  3.828088   \n",
       "A1CF  2.981775  2.947547  2.872071  3.075478  2.853231  3.221491  2.996355   \n",
       "\n",
       "       1240135   1290812    907045  ...    753584    907044    998184  \\\n",
       "GENE                                ...                                 \n",
       "A1BG  5.146903  3.107543  5.062066  ...  4.272172  3.435025  4.930052   \n",
       "A1CF  2.893977  2.755668  2.985650  ...  2.941659  3.155536  2.983619   \n",
       "\n",
       "        908145   1659787   1298157   1480372   1298533    930299  905954.1  \n",
       "GENE                                                                        \n",
       "A1BG  2.900213  4.523712  5.074951  2.957153  3.089628  4.047364  5.329524  \n",
       "A1CF  3.118312  2.975409  2.905804  2.944488  2.780003  2.870819  2.926353  \n",
       "\n",
       "[2 rows x 1018 columns]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gene_exp_df = gene_exp_df.groupby(gene_exp_df.index).mean()\n",
    "print(\"Dataframe shape:\", gene_exp_df.shape, \"\\n\")\n",
    "gene_exp_df.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Normalize gene expression\n",
    "We normalized baseline gene expression values for each gene by computing fold-changes compared to the average value across cell-lines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T00:58:57.267701Z",
     "start_time": "2020-12-04T00:58:53.257323Z"
    }
   },
   "outputs": [],
   "source": [
    "cell_line_log2_mean_fc_exp_df, cell_line_mean_exp_df = pp.gexp.normalize_log2_mean_fc(gene_exp_df)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read essential genes list\n",
    "\n",
    "Or in case you want your training using one specific set of genes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T00:58:59.707850Z",
     "start_time": "2020-12-04T00:58:59.688810Z"
    }
   },
   "outputs": [],
   "source": [
    "ess_gene_list = utility.get_gene_list('../data/essential_genes.txt')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sample with both expression and response data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T00:59:00.575053Z",
     "start_time": "2020-12-04T00:59:00.570718Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "985"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cell_line_sample_list = np.array([s for s in cell_line_sample_list if s in gene_exp_df.columns])\n",
    "len(cell_line_sample_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Arrange gene expression and drug response matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T00:59:03.595931Z",
     "start_time": "2020-12-04T00:59:03.520288Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((17419, 985), (985, 226), (226, 27))"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cell_line_log2_mean_fc_exp_df = cell_line_log2_mean_fc_exp_df[cell_line_sample_list]\n",
    "cell_line_obs_df = cell_line_obs_df.loc[cell_line_sample_list, dataset_drug_list]\n",
    "dataset_drug_df = dataset_drug_df.loc[dataset_drug_list]\n",
    "\n",
    "cell_line_log2_mean_fc_exp_df.shape, cell_line_obs_df.shape, dataset_drug_df.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Calculate kernel feature \n",
    "\n",
    "Based on all given cell line samples with gene expression profiles and a list of genes (e.g. essential gene list). This step might take a bit more time than the usual."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T01:00:55.393887Z",
     "start_time": "2020-12-04T00:59:08.857488Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calculating kernel features based on 1610 common genes\n",
      "(17419, 985) (17419, 985)\n",
      "100 of 985 (12.43)s\n",
      "200 of 985 (12.70)s\n",
      "300 of 985 (12.84)s\n",
      "400 of 985 (12.60)s\n",
      "500 of 985 (12.45)s\n",
      "600 of 985 (12.24)s\n",
      "700 of 985 (8.26)s\n",
      "800 of 985 (8.07)s\n",
      "900 of 985 (8.01)s\n"
     ]
    }
   ],
   "source": [
    "kernel_feature_df = pp.gexp.calculate_kernel_feature(cell_line_log2_mean_fc_exp_df, cell_line_log2_mean_fc_exp_df, ess_gene_list).loc[cell_line_sample_list]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Model training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T01:00:55.404007Z",
     "start_time": "2020-12-04T01:00:55.395329Z"
    }
   },
   "outputs": [],
   "source": [
    "# kernel feature based only on training samples\n",
    "X_train = kernel_feature_df.loc[cell_line_sample_list, cell_line_sample_list]\n",
    "# observed drug response\n",
    "Y_train = cell_line_obs_df.loc[cell_line_sample_list]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T01:00:55.423167Z",
     "start_time": "2020-12-04T01:00:55.405529Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataframe shape: (985, 985) \n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1240121</th>\n",
       "      <th>1240122</th>\n",
       "      <th>1240123</th>\n",
       "      <th>1240124</th>\n",
       "      <th>1240125</th>\n",
       "      <th>1240127</th>\n",
       "      <th>1240128</th>\n",
       "      <th>1240129</th>\n",
       "      <th>1240130</th>\n",
       "      <th>1240131</th>\n",
       "      <th>...</th>\n",
       "      <th>949175</th>\n",
       "      <th>949176</th>\n",
       "      <th>949177</th>\n",
       "      <th>949178</th>\n",
       "      <th>949179</th>\n",
       "      <th>971773</th>\n",
       "      <th>971774</th>\n",
       "      <th>971777</th>\n",
       "      <th>998184</th>\n",
       "      <th>998189</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1240121</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.200762</td>\n",
       "      <td>-0.097257</td>\n",
       "      <td>0.079455</td>\n",
       "      <td>-0.080807</td>\n",
       "      <td>-0.107964</td>\n",
       "      <td>-0.058302</td>\n",
       "      <td>0.079915</td>\n",
       "      <td>0.063199</td>\n",
       "      <td>0.035671</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.129215</td>\n",
       "      <td>-0.179337</td>\n",
       "      <td>-0.095300</td>\n",
       "      <td>-0.112817</td>\n",
       "      <td>-0.186527</td>\n",
       "      <td>-0.088457</td>\n",
       "      <td>-0.143004</td>\n",
       "      <td>-0.189747</td>\n",
       "      <td>-0.259590</td>\n",
       "      <td>-0.054617</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1240122</th>\n",
       "      <td>0.200762</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.193214</td>\n",
       "      <td>-0.049567</td>\n",
       "      <td>-0.180749</td>\n",
       "      <td>0.187601</td>\n",
       "      <td>0.042315</td>\n",
       "      <td>0.171160</td>\n",
       "      <td>-0.049354</td>\n",
       "      <td>-0.061332</td>\n",
       "      <td>...</td>\n",
       "      <td>-0.008915</td>\n",
       "      <td>0.042224</td>\n",
       "      <td>0.080204</td>\n",
       "      <td>0.052032</td>\n",
       "      <td>-0.091817</td>\n",
       "      <td>0.007112</td>\n",
       "      <td>0.046598</td>\n",
       "      <td>0.099549</td>\n",
       "      <td>-0.010853</td>\n",
       "      <td>-0.037156</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 985 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          1240121   1240122   1240123   1240124   1240125   1240127   1240128  \\\n",
       "1240121  1.000000  0.200762 -0.097257  0.079455 -0.080807 -0.107964 -0.058302   \n",
       "1240122  0.200762  1.000000  0.193214 -0.049567 -0.180749  0.187601  0.042315   \n",
       "\n",
       "          1240129   1240130   1240131  ...    949175    949176    949177  \\\n",
       "1240121  0.079915  0.063199  0.035671  ... -0.129215 -0.179337 -0.095300   \n",
       "1240122  0.171160 -0.049354 -0.061332  ... -0.008915  0.042224  0.080204   \n",
       "\n",
       "           949178    949179    971773    971774    971777    998184    998189  \n",
       "1240121 -0.112817 -0.186527 -0.088457 -0.143004 -0.189747 -0.259590 -0.054617  \n",
       "1240122  0.052032 -0.091817  0.007112  0.046598  0.099549 -0.010853 -0.037156  \n",
       "\n",
       "[2 rows x 985 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"Dataframe shape:\", X_train.shape, \"\\n\")\n",
    "X_train.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T01:00:55.440999Z",
     "start_time": "2020-12-04T01:00:55.424322Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataframe shape: (985, 226) \n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Drug ID</th>\n",
       "      <th>1</th>\n",
       "      <th>1001</th>\n",
       "      <th>1003</th>\n",
       "      <th>1004</th>\n",
       "      <th>1005</th>\n",
       "      <th>1006</th>\n",
       "      <th>1007</th>\n",
       "      <th>1008</th>\n",
       "      <th>1009</th>\n",
       "      <th>1010</th>\n",
       "      <th>...</th>\n",
       "      <th>64</th>\n",
       "      <th>71</th>\n",
       "      <th>83</th>\n",
       "      <th>86</th>\n",
       "      <th>87</th>\n",
       "      <th>88</th>\n",
       "      <th>89</th>\n",
       "      <th>9</th>\n",
       "      <th>91</th>\n",
       "      <th>94</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1240121</th>\n",
       "      <td>NaN</td>\n",
       "      <td>8.922340</td>\n",
       "      <td>-9.065271</td>\n",
       "      <td>-8.301324</td>\n",
       "      <td>1.998429</td>\n",
       "      <td>-2.455685</td>\n",
       "      <td>-11.688212</td>\n",
       "      <td>4.329096</td>\n",
       "      <td>9.033478</td>\n",
       "      <td>-0.035476</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1240122</th>\n",
       "      <td>NaN</td>\n",
       "      <td>9.594524</td>\n",
       "      <td>-8.343748</td>\n",
       "      <td>-7.554691</td>\n",
       "      <td>6.033703</td>\n",
       "      <td>-2.145146</td>\n",
       "      <td>-10.332062</td>\n",
       "      <td>4.108168</td>\n",
       "      <td>7.775115</td>\n",
       "      <td>-4.102308</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 226 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "Drug ID   1      1001      1003      1004      1005      1006       1007  \\\n",
       "1240121 NaN  8.922340 -9.065271 -8.301324  1.998429 -2.455685 -11.688212   \n",
       "1240122 NaN  9.594524 -8.343748 -7.554691  6.033703 -2.145146 -10.332062   \n",
       "\n",
       "Drug ID      1008      1009      1010  ...  64  71  83  86  87  88  89   9  \\\n",
       "1240121  4.329096  9.033478 -0.035476  ... NaN NaN NaN NaN NaN NaN NaN NaN   \n",
       "1240122  4.108168  7.775115 -4.102308  ... NaN NaN NaN NaN NaN NaN NaN NaN   \n",
       "\n",
       "Drug ID  91  94  \n",
       "1240121 NaN NaN  \n",
       "1240122 NaN NaN  \n",
       "\n",
       "[2 rows x 226 columns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"Dataframe shape:\", Y_train.shape, \"\\n\")\n",
    "Y_train.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Select CaDRReS training for different objective functions\n",
    "\n",
    "1. `cadrres-wo-sample-bias`: CaDRReS + no bp (bp = sample bias)\n",
    "2. `cadrres-wo-sample-bias-weight`: CaDRReS + no bp + ciu + du (ciu = drug-sample weight w.r.t. maximum dosage, du = indication-specific weight). This is **CaDRReS-Sc** model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T01:02:03.643716Z",
     "start_time": "2020-12-04T01:02:03.636546Z"
    }
   },
   "outputs": [],
   "source": [
    "obj_function = widgets.Dropdown(options=['cadrres-wo-sample-bias', 'cadrres-wo-sample-bias-weight'], description='Objetice function')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T01:02:11.096333Z",
     "start_time": "2020-12-04T01:02:11.092875Z"
    }
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c80c8c0899194245b3ed6e6ff2d02787",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Dropdown(description='Objetice function', options=('cadrres-wo-sample-bias', 'cadrres-wo-sample-bias-weight'),…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(obj_function)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T01:03:03.835499Z",
     "start_time": "2020-12-04T01:03:03.832349Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model_spec_name = obj_function.value # cadrres-wo-sample-bias | cadrres-wo-sample-bias-weight\n",
    "\n",
    "indication_specific_degree = 1 # multiply by 1 = no indication-specific\n",
    "# indication_specific_degree = 10\n",
    "\n",
    "indication_specific_degree"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Specify output directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T01:03:07.915256Z",
     "start_time": "2020-12-04T01:03:07.913156Z"
    }
   },
   "outputs": [],
   "source": [
    "output_dir = '../example_result/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T01:03:09.164801Z",
     "start_time": "2020-12-04T01:03:09.161837Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Results will be saved in  ../example_result/\n"
     ]
    }
   ],
   "source": [
    "if not os.path.exists(output_dir):\n",
    "    os.makedirs(output_dir)\n",
    "\n",
    "print ('Results will be saved in ', output_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Train CaDRReS Model "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Prepare x0 for calculating logistic sample weigh (o_i) based on maximum drug dosage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T01:03:11.295035Z",
     "start_time": "2020-12-04T01:03:11.291238Z"
    }
   },
   "outputs": [],
   "source": [
    "sample_weights_logistic_x0_df = model.get_sample_weights_logistic_x0(dataset_drug_df, 'log2_max_conc', X_train.index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Prepare indication weight (skip for this analysis = set all to 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T01:03:12.408704Z",
     "start_time": "2020-12-04T01:03:12.402750Z"
    }
   },
   "outputs": [],
   "source": [
    "indication_weight_df = pd.DataFrame(np.ones(Y_train.shape), index=Y_train.index, columns=Y_train.columns)\n",
    "cv_cell_line_hn_sample_list = [cl for cl in cell_line_hn_sample_list if cl in X_train.index]\n",
    "indication_weight_df.loc[cv_cell_line_hn_sample_list, :] = indication_weight_df.loc[cv_cell_line_hn_sample_list, :] * indication_specific_degree"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Start model training"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T01:30:57.044618Z",
     "start_time": "2020-12-04T01:03:16.079136Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Getting data ...\n",
      "Initializing the model ...\n",
      "WARNING:tensorflow:From /mnt/volume1/Dropbox/Research/2019_drug_response_heterogeneity/CaDRReS_depository/cadrres_sc/model.py:99: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n",
      "\n",
      "WARNING:tensorflow:From /mnt/volume1/Dropbox/Research/2019_drug_response_heterogeneity/CaDRReS_depository/cadrres_sc/model.py:120: The name tf.truncated_normal is deprecated. Please use tf.random.truncated_normal instead.\n",
      "\n",
      "WARNING:tensorflow:From /mnt/volume1/Dropbox/Research/2019_drug_response_heterogeneity/CaDRReS_depository/cadrres_sc/model.py:122: The name tf.get_variable is deprecated. Please use tf.compat.v1.get_variable instead.\n",
      "\n",
      "Train: 180239 out of 222610\n",
      "Starting model training ...\n",
      "WARNING:tensorflow:From /mnt/volume1/Dropbox/Research/2019_drug_response_heterogeneity/CaDRReS_depository/cadrres_sc/model.py:548: The name tf.train.GradientDescentOptimizer is deprecated. Please use tf.compat.v1.train.GradientDescentOptimizer instead.\n",
      "\n",
      "WARNING:tensorflow:From /mnt/volume1/Dropbox/Research/2019_drug_response_heterogeneity/CaDRReS_depository/cadrres_sc/model.py:550: The name tf.summary.scalar is deprecated. Please use tf.compat.v1.summary.scalar instead.\n",
      "\n",
      "WARNING:tensorflow:From /mnt/volume1/Dropbox/Research/2019_drug_response_heterogeneity/CaDRReS_depository/cadrres_sc/model.py:552: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.\n",
      "\n",
      "TF session started ...\n",
      "WARNING:tensorflow:From /mnt/volume1/Dropbox/Research/2019_drug_response_heterogeneity/CaDRReS_depository/cadrres_sc/model.py:562: The name tf.global_variables_initializer is deprecated. Please use tf.compat.v1.global_variables_initializer instead.\n",
      "\n",
      "Starting 1st iteration ...\n",
      "MSE train at step 0: 35.244 (0.01m)\n",
      "MSE train at step 5000: 12.097 (1.40m)\n",
      "MSE train at step 10000: 5.469 (2.79m)\n",
      "MSE train at step 15000: 4.209 (4.19m)\n",
      "MSE train at step 20000: 3.750 (5.58m)\n",
      "MSE train at step 25000: 3.509 (6.97m)\n",
      "MSE train at step 30000: 3.363 (8.36m)\n",
      "MSE train at step 35000: 3.261 (9.75m)\n",
      "MSE train at step 40000: 3.181 (11.14m)\n",
      "MSE train at step 45000: 3.121 (12.53m)\n",
      "MSE train at step 50000: 3.069 (13.91m)\n",
      "MSE train at step 55000: 3.027 (15.30m)\n",
      "MSE train at step 60000: 2.990 (16.65m)\n",
      "MSE train at step 65000: 2.957 (18.00m)\n",
      "MSE train at step 70000: 2.926 (19.39m)\n",
      "MSE train at step 75000: 2.898 (20.74m)\n",
      "MSE train at step 80000: 2.872 (22.13m)\n",
      "MSE train at step 85000: 2.846 (23.52m)\n",
      "MSE train at step 90000: 2.822 (24.91m)\n",
      "MSE train at step 95000: 2.799 (26.30m)\n",
      "Saving model parameters and predictions ...\n",
      "DONE\n"
     ]
    }
   ],
   "source": [
    "if model_spec_name in ['cadrres', 'cadrres-wo-sample-bias']:\n",
    "    cadrres_model_dict, cadrres_output_dict = model.train_model(Y_train, X_train, Y_train, X_train, 10, 0.0, 100000, 0.01, model_spec_name=model_spec_name, save_interval=5000, output_dir=output_dir)\n",
    "elif model_spec_name in ['cadrres-wo-sample-bias-weight']:\n",
    "    cadrres_model_dict, cadrres_output_dict = model.train_model_logistic_weight(Y_train, X_train, Y_train, X_train, sample_weights_logistic_x0_df, indication_weight_df, 10, 0.0, 100000, 0.01, model_spec_name=model_spec_name, save_interval=5000, output_dir=output_dir)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Save the final CaDRReS Model trained on your dataset\n",
    "\n",
    "- cadrres_model_dict contains model hyperparameters and trained parameters\n",
    "- cadrres_output_dict contains data and prediction on training dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-12-04T01:30:57.094227Z",
     "start_time": "2020-12-04T01:30:57.049565Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Saving ../example_result/cadrres-wo-sample-bias-weight_param_dict.pickle\n",
      "Saving ../example_result/cadrres-wo-sample-bias-weight_output_dict.pickle\n"
     ]
    }
   ],
   "source": [
    "print('Saving ' + output_dir + '{}_param_dict.pickle'.format(model_spec_name))\n",
    "pickle.dump(cadrres_model_dict, open(output_dir + '{}_param_dict.pickle'.format(model_spec_name), 'wb'))\n",
    "print('Saving ' + output_dir + '{}_output_dict.pickle'.format(model_spec_name))\n",
    "pickle.dump(cadrres_output_dict, open(output_dir + '{}_output_dict.pickle'.format(model_spec_name), 'wb'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Next Step\n",
    "After you saved the CaDRReS-Sc model here, you can follow this [tutorial](./prediction_pretrained_model.ipynb) to predict drug response based on the pre-trained model."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "\n",
    "**Authors:** [Chayaporn Suphavilai](mailto:@.com), [Rafael Peres da Silva](), Genome Institute of Singapore, Nagarajan Lab, April 30, 2020\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Reproducibility tips from https://github.com/jupyter-guide/ten-rules-jupyter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
